package com.jcode222.controller;


import com.jcode222.entity.Order;
import com.jcode222.entity.response.ResponseResult;
import com.jcode222.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pdai
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * @param order order param
     * @return order
     */
    @ApiOperation("Add/Edit Order")
    @PostMapping("add")
    public ResponseResult<Order> add(Order order) {
        if (order.getId()==null) {
            orderService.save(order);
        } else {
            orderService.update(order);
        }
        return ResponseResult.success(orderService.findById(order.getId()));
    }


    /**
     * @return order list
     */
    @ApiOperation("Query Order One")
    @GetMapping("edit/{orderId}")
    public ResponseResult<Order> edit(@PathVariable("orderId") Long orderId) {
        return ResponseResult.success(orderService.findById(orderId));
    }

    /**
     * @return order list
     */
    @ApiOperation("Query Order List")
    @GetMapping("list")
    public ResponseResult<List<Order>> list(Order order) {
        return ResponseResult.success(orderService.findList(order));
    }

    /**
     * @param order order param
     * @return order
     */
    @ApiOperation("Create Order")
    @PostMapping("createOrder")
    public ResponseResult<Order> createOrder(Order order) {
        order.setCommodityCode("commodity_code");
        orderService.createOrder(order);
        return ResponseResult.success(order);
    }

    /**
     * @param order order param
     * @return order
     */
    @ApiOperation("Create Order transaction")
    @PostMapping("createOrderTx")
    public ResponseResult<Order> createOrderTransaction(Order order) {
        order.setCommodityCode("commodity_code");
        orderService.createOrderTransaction(order);
        return ResponseResult.success(order);
    }

    /**
     * @param order order param
     * @return order
     */
    @ApiOperation("Create Order transaction exception")
    @PostMapping("createOrderTxEx")
    public ResponseResult<Order> createOrderTransactionException(Order order) {
        order.setCommodityCode("commodity_code");
        orderService.createOrderTransactionException(order);
        return ResponseResult.success(order);
    }
}
